package com.lw.yygh.order.schedule;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lw.yygh.enums.OrderStatusEnum;
import com.lw.yygh.hosp.feign.HospFeignClient;
import com.lw.yygh.model.order.OrderInfo;

import com.lw.yygh.model.order.SignInfoVo;
import com.lw.yygh.order.feign.MsmFeignClient;
import com.lw.yygh.order.service.OrderService;
import com.lw.yygh.utils.HttpRequestHelper;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@EnableAsync
public class OrderSchedule {
    @Autowired
    private OrderService orderService;
    @Autowired
    private HospFeignClient hospFeignClient;
    @Autowired
    private MsmFeignClient msmFeignClient;
    @Async
    @Scheduled(fixedDelay = 120000)
    public void Timer()  {
        //查询还未支付的订单将其删除
        QueryWrapper<OrderInfo> wrapper=new QueryWrapper<>();
        wrapper.eq( "order_status",0 );
        //大于五分钟之前的订单
        Date date = new Date();
        long time = date.getTime();
        time=time-300000;
        Date ltDate = new Date( time );
        wrapper.lt( "create_time",ltDate );
        List<OrderInfo> list = orderService.list( wrapper );
        for (OrderInfo orderInfo:list){
            //更改该订单状态，更改医院那边的订单状态和更新管理医院那边的排班信息(可预约数加1)
            orderService.updateOrder( orderInfo.getId() );
            //获取签名
            SignInfoVo signInfoVo = hospFeignClient.getSignInfoVo( orderInfo.getHoscode() );
            //设置请求参数
            Map<String,Object> reqMap=new HashMap<>();
            reqMap.put("hosRecordId",orderInfo.getHosRecordId());
            reqMap.put("sign", signInfoVo.getSignKey());
            HttpRequestHelper.sendRequest( reqMap, "http://localhost:9998/order/updateOrder" );
            //更新排班
            hospFeignClient.updateSchedule( orderInfo.getScheduleId() );
        }
    }
    //就医提醒
    //cron表达式 设置每天8点发送就医提醒
    @Async
    @Scheduled(cron = "0 0 8 * * ? ")
//    @Scheduled(fixedDelay = 60000)
    public void patientTask(){
        //根据订单状态和取号时间查询当日需要提醒的就诊人
        QueryWrapper<OrderInfo> wrapper=new QueryWrapper<>();
        wrapper.eq( "order_status", OrderStatusEnum.GET_NUMBER.getStatus() ).or().eq( "order_status", OrderStatusEnum.PAID.getStatus() ) ;
        //时间范围
        SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
        Date date = new Date();
        String startTimeString = dateFormat.format( date );
        Date startTime = null;
        try {
            startTime = dateFormat.parse( startTimeString );
            long time = startTime.getTime();
            time=time+86400000;
            Date endTime = new Date( time );
            wrapper.ge( "reserve_date",startTime ).lt( "reserve_date",endTime );
            List<OrderInfo> list = orderService.list( wrapper );
            for (OrderInfo orderInfo:list){
                msmFeignClient.patientTask( orderInfo.getPatientPhone() );
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
}
